import { Application } from 'egg';
import {
    indexRule,
    createRule,
    destroyRule,
    clearRule
} from '../validate/cart';

/**
 * 小程序路由
 */
export default (app: Application) => {
    const { header } = app.middleware;
    const { jwt } = app;

    return {
        'cart.list': {
            method: 'GET',
            path: '/xxm/cart/list',
            options: {
                tags: ['cart/list'],
                description: '购物车列表',
                handler: app.controller.wx.cart.list,
                middlewares: [jwt, header()],
                validate: indexRule,
            },
        },
        'cart.create': {
            method: 'POST',
            path: '/xxm/cart/create',
            options: {
                tags: ['cart/create'],
                description: '购物车列表',
                handler: app.controller.wx.cart.create,
                middlewares: [jwt, header()],
                validate: createRule,
            },
        },
        'cart.destroy': {
            method: 'DELETE',
            path: '/xxm/cart/:id',
            options: {
                tags: ['wx/cart'],
                description: '删除购物车商品',
                handler: app.controller.wx.cart.destroy,
                middlewares: [jwt, header()],
                validate: destroyRule,
            },
        },
        'cart.clear': {
            method: 'DELETE',
            path: '/xxm/cartClear/:user_id',
            options: {
                tags: ['wx/cart/clear'],
                description: '删除购物车商品',
                handler: app.controller.wx.cart.clear,
                middlewares: [jwt, header()],
                validate: clearRule,
            },
        },
    };
};
